package com.qige.dome.homework;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;

/**
 * @Author qige
 * @Date 2024-08-01 19:39
 * @Description
 */
public class homework5 {
    /*
    2.由控制台按照固定格式输入学生信息，包括学号，姓名，年龄信息，当输入的内容为exit退出；
    将输入的学生信息分别封装到一个Student对象中，再将每个Student对象加入到一个集合中，
    要求集合中的元素按照年龄大小正序排序；最后遍历集合
    推荐步骤：
    a)创建Student类，并指定按照年龄正序排列
    b)通过控制台输入多个不同Student信息。格式规定为：编号#姓名#年龄
    c)取出字符串中相应信息放入Student对象，并将Student加入到集合中
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学生信息，格式为：编号#姓名#年龄");
        String input = scanner.nextLine();
        ArrayList<Student1> list = new ArrayList<>();
        while (!input.equals("exit")) {
            String[] split = input.split("#");
            Student1 student = new Student1(split[0], split[1], Integer.valueOf(split[2]));
            list.add(student);
            input = scanner.nextLine();
        }
        list.sort((o1, o2) -> o1.getAge().compareTo(o2.getAge()));
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}
class Student1{
    private String id;
    private String name;
    private Integer age;

    public Student1() {
    }

    public Student1(String id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student1{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student1 student1 = (Student1) o;
        return Objects.equals(id, student1.id) && Objects.equals(name, student1.name) && Objects.equals(age, student1.age);
    }

    @Override
    public int hashCode() {
        int result = Objects.hashCode(id);
        result = 31 * result + Objects.hashCode(name);
        result = 31 * result + Objects.hashCode(age);
        return result;
    }
}